package com.dianrui.operation.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dianrui.operation.Constant;
import com.dianrui.operation.R;
import com.dianrui.operation.okhttp.XHttpRequest;
import com.dianrui.operation.util.SharedUtil;
import com.dianrui.operation.util.StringUtils;
import com.dianrui.operation.util.ToastUtil;
import com.dianrui.operation.util.Url;
import com.dianrui.operation.view.opertionzxing.activity.OpeationCaptureActivity;
import com.google.gson.JsonObject;


import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 盒子上下线
 */
public class DeviceOnlineActivity extends BaseActivity {
    private String titles;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.car_name_input)
    EditText inputImel;


    @Override
    public int getContentViewId() {
        return R.layout.device_online;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                titles = bundle.getString("title");
                title.setText(titles);
            }
        }
    }

    @Override
    public void initData() {}

    @OnClick({R.id.back, R.id.online, R.id.online_off, R.id.scans})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.online:
                if (StringUtils.isEmpty(inputImel.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.input_device_number_error));
                    return;
                }
                startOnline();
                break;

            case R.id.online_off:
                if (StringUtils.isEmpty(inputImel.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.input_device_number_error));
                    return;
                }
                startOnlineOff();
                break;

            case R.id.scans:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (ContextCompat.checkSelfPermission(DeviceOnlineActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                        Intent intent = new Intent(DeviceOnlineActivity.this, OpeationCaptureActivity.class);
                        intent.putExtra("camflag", "1");
                        startActivityForResult(intent, 102);
                    } else {
                        ActivityCompat.requestPermissions(DeviceOnlineActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
                    }
                } else {
                    Intent intent = new Intent(DeviceOnlineActivity.this, OpeationCaptureActivity.class);
                    intent.putExtra("camflag", "1");
                    startActivityForResult(intent, 102);
                }
                break;

            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 102) {
            String imel = data.getStringExtra("IMEI");
            inputImel.setText(imel);
        }
    }

    //设备上线
    private void startOnline() {
        showLoadingDialog("设备上线中...");
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("number", inputImel.getText().toString());
        jsonObject.addProperty("state", "1");//0下线，1上线
        XHttpRequest.getInstance().postRequests(SharedUtil.getUrl() + Url.DEVICE_ONLINE, jsonObject.toString(), new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                dismissLoadingDialog();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1001".equals(jsonObject.optString("status"))) {
                                dismissLoadingDialog();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1002".equals(jsonObject.optString("status"))) {
                                dismissLoadingDialog();
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                dismissLoadingDialog();
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });

    }


    private void startOnlineOff() {
        showLoadingDialog("设备下线中...");
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("number", inputImel.getText().toString());
        jsonObject.addProperty("state", "0");
        XHttpRequest.getInstance().postRequests(SharedUtil.getUrl() + Url.DEVICE_ONLINE, jsonObject.toString(), new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                dismissLoadingDialog();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1001".equals(jsonObject.optString("status"))) {
                                dismissLoadingDialog();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1002".equals(jsonObject.optString("status"))) {
                                dismissLoadingDialog();
                                spUtils.clear();
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                dismissLoadingDialog();
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Intent intent = new Intent(DeviceOnlineActivity.this, OpeationCaptureActivity.class);
                    intent.putExtra("camflag", "1");
                    startActivityForResult(intent, 102);
                }
                break;

            default:
                break;
        }

    }


}
